﻿using System;
using System.Linq;

namespace eCommerce.DataAccess
{
    public class CustomerDA
    {
        public static void UpdateCustomer(Guid currentUserId, string billingFirstName, string billingSurname, string mobile, string emailAddress, string bFirstName, string bSurname, string bPhone)
        {
            try
            {
                eCommerceDataContext dataContext = new eCommerceDataContext();

                var customer = (dataContext.Customers.Where(cu => cu.userID == currentUserId)).FirstOrDefault();
                if (customer != null)
                {
                    customer.FirstName = billingFirstName;
                    customer.Surname = billingSurname;
                    customer.Mobile = mobile;
                    customer.EmailAddress = emailAddress;
                    customer.BFirstName = bFirstName;
                    customer.BSurname = bSurname;
                    customer.BPhone = bPhone;
                    dataContext.SubmitChanges();
                }
                else
                {

                    Customer customerNew = new Customer
                                               {
                                                   FirstName = billingFirstName,
                                                   Surname = billingSurname,
                                                   Mobile = mobile,
                                                   EmailAddress = emailAddress,
                                                   BFirstName = bFirstName,
                                                   BSurname = bSurname,
                                                   BPhone = bPhone,
                                                   userID = currentUserId,
                                                   AlternatePhone = string.Empty
                                               };


                    dataContext.Customers.InsertOnSubmit(customerNew);
                    dataContext.SubmitChanges();
                }

            }
            catch (Exception exception)
            {
                // Need to add error loggin here
                throw exception;
            }
        }

        public static Customer GetCustomer(Guid userId)
        {
            eCommerceDataContext dataContext = new eCommerceDataContext();
            return (dataContext.Customers.Where(c => c.userID == userId)).FirstOrDefault();
        }
    }
}
